<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class UsersController extends AppController {

    var $helpers = array('Html', 'Form');
    var $name = 'Users';
    var $components = array('Uploader.Uploader', 'Auth');

    function index() {
        $this->layout = 'account';
        $this->set('users', $this->User->find('all'));
    }

    function view($id = null) {
        $this->User->id = $id;
        $this->set('user', $this->User->read());
    }

    function beforeFilter() {
      //  $this->Auth->userModel = 'Admin';
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'admin_login');
        $this->Auth->loginRedirect = array('controller' => 'admin', 'action' => 'index'); //action se chuyen den sau khi logi
        $this->Auth->authorize = 'controller';
        $this->Auth->loginError = 'Failed to login'; //thong bao dang nhap bi lo
        $this->Auth->authError = 'Access denied'; //thong bao truy cap khong dung khu vuc            
         $this->Auth->allow('index', 'view');
        $this->Auth->fields = array(
            'username' => 'username',
            'password' => 'password'
        );
      
    }

    function admin_login() {
        
        $this->layout='ajax';
        
    }

    /*
      function beforeFilter(){
      $this->Uploader->uploadDir = 'files/uploads/'; //thu muc chua file upload
      $this->Uploader->enableUpload = true;
      $this->Uploader->maxFileSize = '2M'; // quy dinh dung luong duoc upload len toi da la 2 Megabytes
      $this->Uploader->maxNameLength = 25;//do dai cua ten file
      $this->Uploader->tempDir = TMP;
      } */

    function Add() {
        if (!empty($this->data)) {

            if ($this->User->save($this->data)) {
                $this->Session->setFlash('Your post has been saved.');
                $this->redirect(array('action' => 'index'));
            }
            /* if ($data = $this->Uploader>upload('avatar')) {
              echo "Upload thanh cong";
              }
              else{
              echo "Co loi xay ra! Vui long thu lai!";
              } */
        }
    }

}

?>
